package main

import (
	"bufio"
	"fmt"
	"math"
	"runtime"
	"os"
	"go.uber.org/zap"
)
var aa = 33

var (
	bb int = 1
	cc string = "123"
	dd float32 = 2.01
) 

func variable() {
	var a, d  = 1, 2
	var b int
	var s string = "123"
	fmt.Println(a, d, b, s)
	fmt.Printf("%d %q\n", a, s)
	
	fmt.Println(aa, bb, cc, dd)
}

func consts() {
	const filename string = "123.txt"
	const a, b = 3, 4
	var c int = int(math.Sqrt(a*a + b*b))
	fmt.Println(a, b, filename, c)
}


func enums() {
	// 定义枚举变量
	const(
		cpp = 0
		java = 1
		python = 2
		golang = 3
	)

	// 利用 iota 实现复杂的
	const (
		b = 1 << (10 * iota)
		kb
		mb
		gb
		pb
	)

	fmt.Println(b, kb, mb, gb, pb)
}

func ifcontent() {
	// 支持条件内赋值, 变量的作用域就在 if else语句中
	if a, b := 2, 3; a == 3 {
		fmt.Println(a)
	} else {
		fmt.Println(b)
		fmt.Println("cannot print", nil)
	}
}

func forT(filename string) {
	file, err := os.Open(filename)
	if err != nil {
		panic(err)
	}

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

func div(a, b int, op string) (int, error) {
    if a == 1 {
        return 1, nil
    } else {
        return 0, fmt.Errorf("err....%s", op)  // 出错时，返回异常error
    }
}

func main() {
	fmt.Println(runtime.GOARCH)
	variable()
	consts()
	enums()
	ifcontent()
	forT("abc.txt")
	aaa, err := div(2, 3, "123")
	fmt.Println(aaa, err)

	log, _ := zap.NewProduction()
    log.Warn("warning test")
}
